<!DOCTYPE html>
<html class="x-admin-sm">
    <head>
        <meta charset="UTF-8">
        <title>欢迎页面-X-admin2.2</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="./css/font.css">
        <link rel="stylesheet" href="./css/xadmin.css">
        <script src="./lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./js/xadmin.js"></script>
		<script src="js/jquery.min.js"></script>
        <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="/">首页</a>
          
          </span>
    
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">		
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">
							<!-- 搜索 -->
							<div class="demoTable" style="float:right">
							    用户名：
							    <div class="layui-inline">
							        <input class="layui-input" name="userName" id="userName" autocomplete="off">
							    </div>
							    <button class="layui-btn" id="search">搜索</button>
							</div>
							
							<!-- 添加用户 -->
                            <button class="layui-btn" onclick="xadmin.open('添加用户','./member-add.html',600,400)"><i class="layui-icon"></i>添加</button>
							<!-- 批量删除 -->
							 <button class="layui-btn" id="delAll_btn"><i class="layui-icon"></i>删除</button>
							 <!-- 导出Excel -->
							<a class="layui-btn" href="http://localhost:8080/downLoad">导出</a>
							<!-- 导入Excel -->
							 <button type="button" class="layui-btn" id="test1">
							   <i class="layui-icon">&#xe67c;</i>上传Excel
							 </button>
							 
						</div>
					
                        <div class="layui-card-body layui-table-body layui-table-main">
                            <!-- 表格生成位置 -->
							<table class="layui-table layui-form" id="tabledemo" lay-filter="test" >
                               
                                </tbody>
                            </table>
                        </div>
                       
                    </div>
                </div>
            </div>
        </div> 
    </body>
    <script>
			//加载模块
			 layui.use(['table','layer','form','upload'],function(){
				 //获取对象 
				 var table=layui.table,
				 layer=layui.layer,
				 form=layui.form,
				 $=layui.jquery;
				 
				//上传
				 var upload = layui.upload;
				 //执行实例
				 var uploadInst = upload.render({
				   elem: '#test1',//绑定元素
				   url: 'http://localhost:8080/uploadExcel',//上传接口
				   accept:'file',
				   done: function(res){
				     //上传完毕回调
					 location.reload();
					 //空
				   }
				   ,error: function(d){
				     //请求异常回调
					 layer.msg("上传失败")
				   }
				 });
	 
				 table.render({
					 //表获取id
					 elem:'#tabledemo',
					 //地址
					 url:'http://localhost:8080/getUserPage',
					 //是否开启分页
					 page:true,
					 //每页显示条数
					 limit:3,
					 limits:[2,4,6],
					
					 //修改分页数据
					 request:{
						 pageName:'pageNo',
						 limitName:'pageSize'
					 },
					 //数据格式
					 parseData:function(d){
						 var code_=0;
						 var msg_="";
						 var count_=0;
						 var data_=[];
						 //如果code值是200说明成功
						 if(d.code==200){
							 code_=0;
							 msg_=d.msg;
							 count_=d.data.count;
							 data_=d.data.data;
						 }
						 //返回值
						return{
							code:code_,
							msg:msg_,
							count:count_,
							data:data_
						}
					 },
					 id:"info",
					 //表数据
					 cols:[[
						 {type:'checkbox'},
						 {field:'userId',title:'ID'},
						 {field:'userName',title:'用户名称'},
						 {field:'sex',title:'性别'},
						{field:'phone',title:'手机'},
						{field:'email',title:'邮箱'},
						{field:'address',title:'地址'},
						{field:'createTime',title:'加入时间'},
						 {title:'操作',templet:'#abutton'}
						 
					 ]]
					 
				 });
					
				//绑定工具条
				table.on('tool(test)',function(d){
					//获取当前行数据
					console.info("----->",d);
					//获取当前触发时间
					var e=d.event;
					if(e=='delete'){
						layer.confirm("是否要删除该用户？",function(index){
							layer.close(index);//关闭当前弹出层
							//调用AJAX进行删除
							$.getJSON('http://localhost:8080/deleUser',{userId:d.data.userId},function(m){
								if(m.code==200){
									d.del();
								}else{
									layer.msg(d.msg)
								}
							})
						});
					}
				});
					
		//批量删除
		$("#delAll_btn").click(function () {
		    var checkStatus = table.checkStatus('info');
		      var  data = checkStatus.data;
				console.log("===>",data);
		      var  userId = "";
				
		    if (data.length > 0) {
		        for (var i in data) {
		            userId += data[i].userId + ",";
		        }
		        console.log(userId);
		        layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
		            $.post('http://localhost:8080/deleteBatch', {ids: userId}, function (data) {
		                layer.msg(data.msg);
						location.reload();
		                layer.close(index);
		            });
		        })
		    } else {
		        layer.msg("请选择需要删除的用户");
		    }
		});	
			//搜索
	
			
		});
		
		
		
    </script>
	
	<script type="text/jscript" id="abutton">
				<button type="button" class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</button>
				
			</script>
</html>